package com.zjc.springboot.file.controller;

import com.zjc.springboot.file.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class FileController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;

    @Autowired
    private FileService fileService;

    /**
     * 单个文件上传，支持断点续传
     */
    @PostMapping("/upload")
    public void upload(@RequestBody MultipartFile file) {
        try {
            fileService.upload(request, response, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 普通文件下载
     */
    @GetMapping("/download")
    public void download(@RequestParam String fileRealName) throws IOException {
        fileService.download(request, response, fileRealName);
    }

}
